<?php
/*
 * Created on 26-04-2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 require_once("DS.php");
 
 class Entanglement
 {
 	public $synchId;
 	
 	public $userAID;
 	public $userBID;
 	
 	public $userAKey;
 	public $userBKey;
 	
 	function __construct($key) {
 		$this->synchId = $key;
 	}
 	
 	public function AddUser($key){
 		if($this->userAID == null){
 			$this->userAID = $this->GetNewUserID();
 			$this->userAKey = $key;
 			return $this->userAID;
 		}
 		if($this->userBID == null){
 			if($this->userAKey == $key){
 				die(constant('USER_ALREADY_SYNCHED').":$this->userAID");
 			}
 			$this->userBID = $this->GetNewUserID();
			$this->userBKey = $key;
 			return $this->userBID;
 		}
 		// Maybe user is trying to re-sync?
 		if($this->userAKey == $key){
 				die(constant('USER_ALREADY_SYNCHED').":$this->userAID");
 			}
 		if($this->userBKey == $key){
 				die(constant('USER_ALREADY_SYNCHED').":$this->userAID");
 			}
 		// I guess no.
 		die(constant('ENTANGLEMENT_FULL'));
 	}
 	
 	private static function GetNewUserID()
 	{
 		Open();
 		do{
 			$newId = mt_rand();
 			$r = Run("SELECT * FROM tbl_entanglements WHERE UserID_A = '$newId' OR UserID_B = '$newId'");
 			$rowNum = mysql_numrows($r);
 			if($rowNum > 0){
 				$getNext = true;
 			}else{
 				$getNext = false;
 			}
 		} while($getNext);
 		Close();
 		return $newId;
 	}
 	
 	public static function Get($synchKey)
 	{
 		$e = new Entanglement($synchKey);
 		Open();
 		$synchKey = mysql_real_escape_string($synchKey);
 		$eResult = Run("SELECT * FROM tbl_entanglements NATURAL JOIN tbl_keys WHERE SynchKey = '$synchKey'");
 		Close();
 		$rowNum = mysql_numrows($eResult);
 		if($rowNum != 1){
 			// Create new.
 			return $e;
 		}
		$data = mysql_fetch_assoc($eResult);
		if(!is_null($data['UserID_A'])){
			$e->userAID = $data['UserID_A'];
			if(is_null($data['PubKeyA'])){
				die(constant("DATA_CORRUPTED").":ID_WITHOUT_KEY");
			}
			$e->userAKey = $data['PubKeyA'];
		}
		if(!is_null($data['UserID_B'])){
			$e->userBID = $data['UserID_B'];
			if(is_null($data['PubKeyB'])){
				die(constant("DATA_CORRUPTED").":ID_WITHOUT_KEY");
			}
			$e->userBKey = $data['PubKeyB'];
		}
		return $e;
 	}
 	public static function GetEID($userId){
 		Open();
 		$userId = mysql_real_escape_string($userId);
 		$eResult = Run("SELECT E_ID FROM tbl_entanglements NATURAL JOIN tbl_keys WHERE UserID_A = '$userId' OR UserID_B = '$userId'");
 		Close();
 		$rowNum = mysql_numrows($eResult);
 		if($rowNum != 1){
 			// No records.
 			return null;
 		}
		$eID = mysql_result($eResult,0,"E_ID");
		return $eID;
 	}
 	public static function GetFromUserId($userId)
 	{
 		$e = new Entanglement(0);
 		Open();
 		$userId = mysql_real_escape_string($userId);
 		$eResult = Run("SELECT * FROM tbl_entanglements NATURAL JOIN tbl_keys WHERE UserID_A = '$userId' OR UserID_B = '$userId'");
 		Close();
 		$rowNum = mysql_numrows($eResult);
 		if($rowNum != 1){
 			// No records.
 			return null;
 		}
		$data = mysql_fetch_assoc($eResult);
		if(!is_null($data['UserID_A'])){
			$e->userAID = $data['UserID_A'];
			if(is_null($data['PubKeyA'])){
				die(constant("DATA_CORRUPTED").":ID_WITHOUT_KEY");
			}
			$e->userAKey = $data['PubKeyA'];
		}
		if(!is_null($data['UserID_B'])){
			$e->userBID = $data['UserID_B'];
			if(is_null($data['PubKeyB'])){
				die(constant("DATA_CORRUPTED").":ID_WITHOUT_KEY");
			}
			$e->userBKey = $data['PubKeyB'];
		}
		if(!is_null($data['SynchKey'])){
			$e->synchId = $data['SynchKey'];
		}
		return $e;
 	}
 	public function Put(){
		Open();
	 	$r = Run("SELECT E_ID FROM tbl_entanglements WHERE SynchKey = '$this->synchId'");
		if($r == null || mysql_numrows($r)==0){
			Run("INSERT INTO tbl_entanglements(SynchKey) VALUES ('$this->synchId')");
			$r =  Run("SELECT E_ID FROM tbl_entanglements WHERE SynchKey = '$this->synchId'");
			if($r == null){
				die(constant('TECH_ERROR').':Cannot insert data into tbl_entanglements');
			}
		}
		$eID = mysql_result($r,0,"E_ID");
		
		$r = Run("SELECT E_ID FROM tbl_keys WHERE E_ID = '$eID'");
		if(mysql_numrows($r)==0){
			Run("INSERT INTO tbl_keys(E_ID) VALUES ('$eID')");
		}
		
 		if(!is_null($this->userAID)){
 			if(is_null($this->userAKey)){
 				Close();
				die(constant('DATA_CORRUPTED').':User A id is set, but not key.');
 			}
			Run("UPDATE tbl_entanglements SET UserID_A='$this->userAID' WHERE E_ID = '$eID'");
			$k = mysql_real_escape_string($this->userAKey);
			Run("UPDATE tbl_keys SET PubKeyA = '$k' WHERE E_ID ='$eID'");
 		}
 		if(!is_null($this->userBID)){
 			if(is_null($this->userBKey)){
 				Close();
				die(constant('DATA_CORRUPTED').':User B id is set, but not key.');
 			}
			Run("UPDATE tbl_entanglements SET UserID_B='$this->userBID' WHERE E_ID = '$eID'");
			$k = mysql_real_escape_string($this->userBKey);
			RUN("UPDATE tbl_keys SET PubKeyB = '$k' WHERE E_ID ='$eID'");
 		}
 		Close();
 		return true;
 	}
 }
?>
